﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Threading;

namespace TerrariaSvcCtrl {
    internal class MOTDWorker {
        int broadcastDelayMinutes;
        public bool KeepAlive { get; set; }

        string cs = @"server=localhost;userid=svenjd;password=Jolene;database=terraria";
        MySqlConnection conn = null;

        public event MOTDMessage onMOTDTick;
        public delegate void MOTDMessage(string message);


        /// <summary>
        /// Work for MOTDWorker...
        /// </summary>
        /// <param name="broadcastDelay">Delay to start/repeat (in minutes) random MOTDs.</param>
        public void DoWork(object broadcastDelay) {
            this.KeepAlive = true;
            broadcastDelayMinutes = (int)broadcastDelay; //does nothing atm
            Sample();
        }


        void Sample() {

            ////MOTDSleepTimer
            int motdSleepTimer = 5;//1 minute default
            //using (MySqlConnection con = new MySqlConnection(cs)) {
            //    try {
            //        con.Open();
            //        MySqlCommand cmd = new MySqlCommand("sp_GetMOTDTimer", con);
            //        cmd.CommandType = System.Data.CommandType.StoredProcedure;
            //        motdSleepTimer = (int)cmd.ExecuteScalar();
            //    } catch (MySqlException ex) {
            //        Console.WriteLine("Error: {0}", ex.ToString());
            //    } finally {
            //        if (conn != null) {
            //            conn.Close();
            //        }
            //    }
            //}



            while (this.KeepAlive) {

                using (MySqlConnection con = new MySqlConnection(cs)) {
                    try {
                        con.Open();
                        MySqlCommand cmd = new MySqlCommand("sp_GetMOTDTimer", con);
                        cmd.CommandType = System.Data.CommandType.StoredProcedure;
                        motdSleepTimer = Int32.Parse((string)cmd.ExecuteScalar());


                    } catch (MySqlException ex) {
                        Console.WriteLine("Error: {0}", ex.ToString());
                    } catch (Exception er) {
                        Console.WriteLine("Error: {0}", er.ToString());
                    }
                }



                try {
                    Thread.Sleep(motdSleepTimer * (60 * 1000));
                } catch (Exception err) {
                    Console.Error.WriteLine(err);
                }

                string output = "<No database entries/connection>";
                using (MySqlConnection con = new MySqlConnection(cs)) {
                    try {
                        con.Open();
                        MySqlCommand cmd = new MySqlCommand("sp_GetOldestMOTD", con);
                        cmd.CommandType = System.Data.CommandType.StoredProcedure;
                        output = (string)cmd.ExecuteScalar();
                    } catch (MySqlException ex) {
                        Console.WriteLine("Error: {0}", ex.ToString());
                    } finally {
                        if (conn != null) {
                            conn.Close();
                        }
                    }
                }

                if (onMOTDTick != null) {
                    onMOTDTick(output);
                }

            }//end while


        }



    }
}
